package com.xncoding.websocket.WebMvc.demoStomp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@RestController
@Slf4j
public class WSController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /////////////////////全域广播///////////////////////////
    /**
     * 客户端第一次订阅，会返回once信息
     */
    @SubscribeMapping("/topic/v1/broadcast/all")
    public ResultDto<MsgDto> broadcastWithReturnMsg(Principal principal) {
        MsgDto result = new MsgDto();
        result.setFrom("Server");
        result.setTo(principal.getName());
        result.setMsg(principal.getName() + "首次订阅全局消息，此消息本次连接断开前不会再显示");
        return ResultDto.success(result);
    }

    /**
     * 与broadcastWithReturnMsg配套的发送方法
     */
    @MessageMapping("/v1/sendMsgToAll")
    public void sendMsgToAllV1(MsgDto param, Principal principal) {
        MsgDto result = new MsgDto();
        result.setFrom(principal.getName());
        result.setTo("Server");
        result.setMsg(principal.getName() + " 对 所有人说（V1）：" + param.getMsg());
        simpMessagingTemplate.convertAndSend("/topic/v1/broadcast/all", ResultDto.success(result));
    }

    /**
     * 客户端第一次订阅/topic/v2/broadcast，不会返回信息。
     * 除此之外，此写法等同于broadcastWithReturnMsg+sendMsgToAllV1。
     */
    @MessageMapping("/v2/sendMsgToAll")
    @SendTo("/topic/v2/broadcast/all")
    public ResultDto<MsgDto> sendMsgToAllV2(MsgDto param, Principal principal) {
        MsgDto result = new MsgDto();
        result.setFrom(principal.getName());
        result.setTo("Server");
        result.setMsg(principal.getName() + " 对 所有人说（V2）：" + param.getMsg());
        return ResultDto.success(result);
    }


    ///////////////////组内广播/////////////////////
    @MessageMapping("/sendMsgToOrg/{orgNo}")
    @SendTo("/topic/broadcast/{orgNo}")
    public ResultDto<MsgDto> sendMsgToOrg(MsgDto param, Principal principal, @DestinationVariable(value = "orgNo") String orgNo) {
        MsgDto result = new MsgDto();
        result.setFrom(principal.getName());
        result.setTo(orgNo);
        result.setMsg(principal.getName() + " 对 " + orgNo + "组内所有人说：" + param.getMsg());
        return ResultDto.success(result);
    }

    ///////////////////1对1/////////////////////
    @MessageMapping("/v1/sendMsgToSomeOne/{to}")
    public void sendMsgToSomeOneV1(MsgDto param, Principal principal, @DestinationVariable(value = "to") String to) {
        MsgDto result = new MsgDto();
        result.setFrom(principal.getName());
        result.setTo(to);
        result.setMsg(principal.getName() + " 对 " + to + "私聊说（V1）：" + param.getMsg());
        simpMessagingTemplate.convertAndSendToUser(to, "/topic/v1/mymsg", ResultDto.success(result));
    }
}